package com.heng.thread;

/**
 * 线程常用方法：
 * 1、join方法：插队，优先执行该线程(2个线程的时候可以看到效果)
 * 2、yield方法：线程礼让,属于Thread类中的静态方法，表示先让其他线程执行
 * 3、stop方法：手动介绍当前线程
 *
 * @date 2022年01月29日08:24:15
 */
public class TestCarTread {

    public static void main(String[] args) throws InterruptedException {
        CarRunnable car1 = new CarRunnable("奔驰*************");
        CarRunnable car2 = new CarRunnable("宝马");
        CarRunnable car3 = new CarRunnable("奥迪");

        Thread thread1 = new Thread(car1);

        thread1.start();
        System.out.println(thread1.isAlive());
        for (int i = 0; i < 20; i++) {
            if (i == 15) {
                //1、强行限制性该线程
                thread1.join();
            }
            //2、礼让线程
            Thread.yield();
            if (i == 17) {
                System.out.println("线程即将结束：" + i);
                //3、手动结束掉当前线程
                Thread.currentThread().stop();
            }
            System.out.println("main---------------" + i);
        }

    }

    static class CarRunnable implements Runnable {
        String threadName;

        public CarRunnable() {
        }

        public CarRunnable(String threadName) {
            this.threadName = threadName;
        }

        @Override
        public void run() {
            Thread.currentThread().setName(threadName);
            for (int i = 1; i <= 5; i++) {
                System.out.println(Thread.currentThread().getName() + "正在跑。。。。。。。" + i);

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
